package rtda

import (
	"jvm/rtda/heap"
	. "jvm/typedef"
)

const vmStackCapacity = 1024

type Thread struct {
	pc    U4
	stack vmStack
}

func NewThread() *Thread {
	thread := &Thread{
		pc:    0,
		stack: *NewVMStack(vmStackCapacity),
	}
	return thread
}
func (self *Thread) IsStackEmpty() bool {
	return self.stack.IsEmpty()
}
func (self *Thread) CurrentFrame() *Frame {
	return self.stack.Top()
}
func (self *Thread) PushFrame(frame *Frame) {
	self.stack.Push(frame)
}
func (self *Thread) PopFrame() *Frame {
	return self.stack.Pop()
}

func (self *Thread) NewFrame(method *heap.Method) *Frame {
	return &Frame{
		localVars: newLocalVars(method.MaxLocals()),
		opStack:   *newOPStack(method.MaxStack()),
		nextPC:    0,
		thread:    self,
		method:    method,
	}
}

// getter
func (self *Thread) PC() U4 {
	return self.pc

}

// setter
func (self *Thread) SetPC(pc U4) {
	self.pc = pc

}
